home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / LISTBOX.CPP < prev    next >
C/C++ Source or Header  |  1992-12-13  |  4KB  |  170 lines

  1. // ------------ listbox.cpp
  2.  
  3. #include "listbox.h"
  4. #include "keyboard.h"
  5.  
  6. // ----------- common constructor code
  7. void ListBox::OpenWindow()
  8. {
  9.     windowtype = ListboxWindow;
  10.     if (windowstate == CLOSED)
  11.         TextBox::OpenWindow();
  12.     selection = -1;
  13.     addmode = False;
  14.     anchorpoint = -1;
  15.     selectcount = 0;
  16.     SetColors();
  17. }
  18.  
  19. // -------- set the fg/bg colors for the window 
  20. void ListBox::SetColors()
  21. {
  22.     colors.fg = YELLOW;
  23.     colors.bg = BLUE;
  24.     colors.sfg = LIGHTGRAY;
  25.     colors.sbg = BLACK;
  26.     colors.ffg = LIGHTGRAY;
  27.     colors.fbg = BLUE;
  28.     colors.hfg = BLACK;
  29.     colors.hbg = LIGHTGRAY;
  30. }
  31.  
  32. void ListBox::CloseWindow()
  33. {
  34.     TextBox::CloseWindow();
  35. }
  36.  
  37. void ListBox::ClearSelection()
  38. {
  39.     if (selection != -1)
  40.         WriteTextLine(selection, colors.fg, colors.bg);
  41. }
  42.  
  43. void ListBox::SetSelection(int sel)
  44. {
  45.     ClearSelection();
  46.     if (sel >= 0 && sel < wlines)    {
  47.         selection = sel;
  48.         WriteTextLine(sel, colors.sfg, colors.sbg);
  49.     }
  50. }
  51.  
  52. void ListBox::Keyboard(int key)
  53. {
  54.     int sel = selection; // (ClearSelection changes selection)
  55.     switch (key)    {
  56.         case UP:
  57.             if (sel > 0)    {
  58.                 ClearSelection();
  59.                 if (sel == wtop)
  60.                     ScrollDown();
  61.                 SetSelection(sel-1);
  62.             }
  63.             return;
  64.         case DN:
  65.             if (sel < wlines-1)    {
  66.                 ClearSelection();
  67.                 if (sel == wtop+ClientHeight()-1)
  68.                     ScrollUp();
  69.                 SetSelection(sel+1);
  70.             }
  71.             return;
  72.         case '\r':
  73.             Choose();
  74.             return;
  75.         default:
  76.             break;
  77.     }
  78.     TextBox::Keyboard(key);
  79. }
  80.  
  81. // ---------- paint the listbox
  82. void ListBox::Paint()
  83. {
  84.     TextBox::Paint();
  85.     if (text != NULL)
  86.         WriteTextLine(selection, colors.sfg, colors.sbg);
  87. }
  88.  
  89. void ListBox::ScrollUp()
  90. {
  91.     TextBox::ScrollUp();
  92.     WriteTextLine(selection, colors.sfg, colors.sbg);
  93. }
  94.  
  95. void ListBox::ScrollDown()
  96. {
  97.     TextBox::ScrollDown();
  98.     WriteTextLine(selection, colors.sfg, colors.sbg);
  99. }
  100.  
  101. void ListBox::ScrollRight()
  102. {
  103.     TextBox::ScrollRight();
  104.     WriteTextLine(selection, colors.sfg, colors.sbg);
  105. }
  106.  
  107. void ListBox::ScrollLeft()
  108. {
  109.     TextBox::ScrollLeft();
  110.     WriteTextLine(selection, colors.sfg, colors.sbg);
  111. }
  112.  
  113. void ListBox::PageUp()
  114. {
  115.     TextBox::PageUp();
  116.     WriteTextLine(selection, colors.sfg, colors.sbg);
  117. }
  118.  
  119. void ListBox::PageDown()
  120. {
  121.     TextBox::PageDown();
  122.     WriteTextLine(selection, colors.sfg, colors.sbg);
  123. }
  124.  
  125. void ListBox::PageRight()
  126. {
  127.     TextBox::PageRight();
  128.     WriteTextLine(selection, colors.sfg, colors.sbg);
  129. }
  130.  
  131. void ListBox::PageLeft()
  132. {
  133.     TextBox::PageLeft();
  134.     WriteTextLine(selection, colors.sfg, colors.sbg);
  135. }
  136.  
  137. // ---------- Left mouse button was clicked
  138. void ListBox::LeftButton(int mx, int my)
  139. {
  140.        if (my != prevmouseline)    {
  141.         if (ClientRect().Inside(mx, my))    {
  142.             int y = my - ClientTop();
  143.             if (wlines && y < wlines-wtop)
  144.                 SetSelection(wtop+y);
  145.         }
  146.     }
  147.     DFWindow::LeftButton(mx, my);
  148. }
  149.  
  150. void ListBox::DoubleClick(int mx, int my)
  151. {
  152.     if (ClientRect().Inside(mx, my))    {
  153.         my -= ClientTop();
  154.         if (wlines && my < wlines-wtop)
  155.             Choose();
  156.     }
  157.     DFWindow::DoubleClick(mx, my);
  158. }
  159.  
  160. void ListBox::ButtonReleased(int, int)
  161. {
  162.     prevmouseline = -1;
  163. }
  164.  
  165. void ListBox::Choose()
  166. {
  167.     // --- does nothing yet
  168. }
  169.  
  170.